﻿using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.SetUpScale;
using SlaughterItem.Common.Domains.Warehouse;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.GetScaleData
{
    /// <summary>
    /// 获取磅秤表数据
    /// </summary>
    public class GetScaleDataHandler : IRequestHandler<GetScaleDataCommand, Result>
    {
        public IBaseRepository<scaleContexts> ScaleContextRepositoryGet;
        public IBaseRepository<ScaleType> ScaleTypeRepositoryGet;
        public IBaseRepository<DepotType> DepotTypeDataRepositoryGet;

        public GetScaleDataHandler(IBaseRepository<DepotType> DepotTypeDataRepositoryGet, IBaseRepository<scaleContexts> scaleContextRepositoryGet, IBaseRepository<ScaleType> scaleTypeRepositoryGet, IBaseRepository<WarehouseData> warehouseDataRepositoryGet)
        {
            ScaleContextRepositoryGet = scaleContextRepositoryGet;
            ScaleTypeRepositoryGet = scaleTypeRepositoryGet;
            this.DepotTypeDataRepositoryGet = DepotTypeDataRepositoryGet;
        }

        public async Task<Result> Handle(GetScaleDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            var list=(
                        from a in ScaleContextRepositoryGet.GetAll()
                        join b in ScaleTypeRepositoryGet.GetAll()
                        on a.ScaleTypeId equals b.ScaleTypeId
                        join c in DepotTypeDataRepositoryGet.GetAll()
                        on a.BindDepotId equals c.DepotTypeId
                        where a.IsScaleDelete==false
                        select new ShowScaleDataDTO
                        {
                            ScaleId = a.ScaleId,
                            ScaleTypeId=a.ScaleTypeId,
                            ScaleName=a.ScaleName,
                            IsScaleState=a.IsScaleState,
                            TareNum=a.TareNum,
                            IsTareAdd=a.IsTareAdd,
                            BindDepotId=a.BindDepotId,
                            IsGoDepot=a.IsGoDepot,
                            UPCDataName=a.UPCDataName,
                            OperatorName=a.OperatorName,
                            ScaleCreateTime=a.ScaleCreateTime,
                            Modifier=a.Modifier,
                            RevempTime=a.RevempTime,
                            IsScaleDelete=a.IsScaleDelete,
                            IsScaleDisable=a.IsScaleDisable,
                            ScaleTypeName=b.ScaleTypeName,
                            DepotTypeName=c.DepotTypeName
                        }
                    ).ToList();
            if (!string.IsNullOrEmpty(request.ScaleName))
            {
                list = list.Where(x => x.ScaleName.Contains(request.ScaleName)).ToList();
            }
            if (list != null)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Data = list;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
